//
//  HDJumpAnnotationView.m
//  HeiLongJiangScience
//
//  Created by HDHaoShaoPeng on 2017/7/5.
//  Copyright © 2017年 HDHaoShaoPeng. All rights reserved.
//

#import "HDJumpAnnotationView.h"

#import "HDCallOutView.h"
#import <QuartzCore/QuartzCore.h>
#import "HDDeclare.h"
#import "UIView+Extension.h"
//#import "HDScrollLabel.h"

#import "UIButton+WebCache.h"


static NSString *pointHei = @"mapPointHei";
static NSString *pointNor = @"mapPointNor";



@implementation HDJumpAnnotationView

- (id)initWithAnnotation:(HDAnnotation *)annotation animated:(BOOL)animate {
    
    if ((self = [super initWithFrame:CGRectZero])) {
        
        self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.myButton.frame = CGRectZero;
        [self addSubview:self.myButton];
        
        self.annotation = annotation;
        
        float pinWidth = 30;
        float pinHeight = pinWidth * 1.25;
        
        self.userInteractionEnabled = YES;
        self.clipsToBounds = NO;
        [self.myButton setImage:[UIImage imageNamed:@"pinRed"] forState:UIControlStateNormal];
//        [self.myButton sd_setBackgroundImageWithURL:[NSURL URLWithString:self.annotation.imageUrl] forState:UIControlStateNormal];
        [self.myButton addTarget:self action:@selector(showCallOut) forControlEvents:UIControlEventTouchUpInside];
        
        self.frame = CGRectMake(0, 0, pinWidth, pinHeight);
        self.center = self.annotation.point;
        
        self.myButton.frame = CGRectMake(0, 0, pinWidth, pinHeight);
        
//        if (annotation.mineAnn) {
//            self.enabled = NO;
//            self.frame = CGRectMake(0, 0, pinWidth, pinWidth);
//            [self setBackgroundImage:[UIImage imageNamed:@"mapLocationPin"] forState:UIControlStateNormal];
//            self.center = self.annotation.point;
//            
//            [self shakeToShow:self];
//        }
//        else
//        {
//            self.frame = CGRectMake(0, 0, pinWidth, pinHeight);
//            self.center = self.annotation.point;
            //            CABasicAnimation *pindrop = [CABasicAnimation animationWithKeyPath:@"position.y"];
            //            pindrop.duration       = 0.5f;
            //            pindrop.fromValue      = [NSNumber numberWithFloat:self.center.y - kScreenHeight];
            //            pindrop.toValue        = [NSNumber numberWithFloat:self.center.y];
            //            pindrop.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
            //            [self.layer addAnimation:pindrop forKey:@"pindrop"];
//        }
    }
    
    
    return self;
}


-(void)showCallOut
{
    [self.myDelegate heighlightAnnotationWith:self];
    
    self.myButton.transform = CGAffineTransformMakeScale(1.5, 1.5);
}

-(void)showHeigh
{
    self.myButton.transform = CGAffineTransformMakeScale(1.5, 1.5);
}

-(void)showNormal
{
    self.myButton.transform = CGAffineTransformMakeScale(1.0, 1.0);
}



#pragma mark - 动画方法
-(void)startAnimationWith:(float)bili
{
    self.hidden = NO;
    
    CABasicAnimation *scaoleAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    scaoleAnimation.duration = 0.8;
    scaoleAnimation.autoreverses = YES;
    scaoleAnimation.repeatCount = MAXFLOAT;
    // 设定动画起始帧和结束帧
    //    scaoleAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y)]; // 起始点
    //    scaoleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.center.x, self.center.y + 10)]; // 终了点
    
    scaoleAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0)]; // 起始点
    scaoleAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0 + 10)];
    
    
    [self.myButton.layer addAnimation:scaoleAnimation forKey:@"tiaoAnimation"];
    
    scaoleAnimation.removedOnCompletion = NO;
    scaoleAnimation.fillMode = kCAFillModeForwards;
}

-(void)stopAnimation
{
    [self.myButton.layer removeAllAnimations];
}





#pragma mark - 观察者方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    if ([keyPath isEqual:@"contentSize"]) {
        NSString *biliStr = (NSString*)[object valueForKey:@"zoomScale"];
        float bili = biliStr.floatValue;
        self.center = CGPointMake(self.annotation.point.x*bili, self.annotation.point.y*bili);
        
        
        
        if (self.isAnimation) {
            
            [self.myButton.layer removeAllAnimations];
            
            [self startAnimationWith:bili];
            
//            if (_canStartAnimation) {
//                _canStartAnimation = NO;
//                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//                    sleep(0.1);
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        [self startAnimationWith:_lastBili];
//                        _canStartAnimation = YES;
//                    });
//                });
//            }
        }
        else
        {
            
        }
        
    }
}








/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
